<template>
  <div>
    <h1>{{id ? '编辑':'新增'}}普通管理员</h1>
    <el-form @submit.native.prevent="save">
      <el-form-item label="账号">
        <el-input v-model="admins.account"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="admins.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" native-type="submit">{{id ? '编辑':'新增'}}管理员</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  export default {
    props:{
      id:{}
    },
    data(){
      return{
        admins:{
          power:2,
          state:1
        },
        power:0
      }
    },
    created(){
      this.id && this.fetch();
    },
    methods:{
      async save(){
        let res;
        if(this.id){
          res=await this.$http.put(`/admins/addAdmins/${this.id}`,this.admins);
        }else{
          res=await this.$http.post("/admins/addAdmins",this.admins);
        }
        this.$router.push("/admins/adminlist");
        this.$message({
          type:'success',
          message:'保存成功'
        });
      },
      async fetch(){
        const res=await this.$http.get(`/admins/addAdmins/${this.id}`);
        this.admins=res.data;
      }
    }
  }
</script>

<style lang="scss" scoped>

</style>